package webgamemaker.FightCanvas;

import java.util.TreeMap;

import webgamemaker.Common.ExtendedGWTCanvas;

import com.google.gwt.core.client.JsArrayInteger;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;

public class FightCanvas extends GWTCanvas
{
	public Team listTeam[];
	public int width, height;
	public Background background;
	public TreeMap<Integer,Layer> listLayer;
	public int frame;
	public ExtendedGWTCanvas selectCanvas;
	public Fighter fighterSelected;
	
	public FightCanvas()
	{
		selectCanvas = new ExtendedGWTCanvas();
		listTeam = new Team[2];
		for(int i = 0; i < 2; i++)
		{
			listTeam[i] = new Team();
		}
		listLayer = new TreeMap<Integer, Layer>();
		this.addDomHandler(new MouseDownHandler()
		{
			public void onMouseDown(MouseDownEvent event) 
			{
				int x = (event.getClientX()-FightCanvas.this.getAbsoluteLeft());
				int y = (event.getClientY()-FightCanvas.this.getAbsoluteTop());
				Selectable select = findSelected(x,y);
				if(select != null && select instanceof Fighter)
				{
					if(fighterSelected != null)
						fighterSelected.select = false;
					fighterSelected = (Fighter)select;
					fighterSelected.select = true;
					((Fighter)select).onSelect();
				}
			}
			
		}, MouseDownEvent.getType());
		Timer timer = new Timer(){
			public void run() 
			{
				frame++;
				redraw();
				this.schedule(10);
			}};
		timer.schedule(50);
	}
	
	public void setCoordSize(int width, int height)
	{
		super.setCoordSize(width, height);
		if(selectCanvas != null)
			selectCanvas.setCoordSize(width, height);
	}
	
	public void redraw()
	{
		width = getOffsetWidth();
		height = getOffsetHeight();
		draw();
	}
	
	public void draw()
	{
		listLayer.clear();
		if(background != null)
			addDrawable(background,0);
		drawTeam(listTeam[0], width/10, 2*height/3, width/3, height/3, false);
		drawTeam(listTeam[1], 9*width/10 - width/3, 2*height/3, width/3, height/3, true);
		clear();
		for(Layer layer : listLayer.values())
			layer.draw(this);
	}
	
	public void drawTeam(Team team, int x, int y, int width, int height, boolean mirror)
	{
		int nbPerLine = 4;
		int nbLine = (team.listFighter.size()-1) / nbPerLine + 1;
		int subWidth = width / nbLine;
		for(int i = 0; i < team.listFighter.size(); i++)
		{
			Fighter fighter = team.listFighter.get(i);
			int posX = (nbLine-(i/nbPerLine)-1)*subWidth + (nbPerLine-(i%nbPerLine)-1)*subWidth/nbPerLine;
			int posY = (i%nbPerLine)*height/nbPerLine;
			if(mirror)
				posX = width-posX;
			fighter.param.put("mirrorX", mirror);
			fighter.standartX = posX+x;
			fighter.standartY = posY+y;
			fighter.standartDepth = 1000+(i%nbPerLine)*25;
			fighter.calcPosition();
			addDrawable(fighter,fighter.depth);
		}
	}
	
	public void addDrawable(Drawable drawable, int depth)
	{
		if(!listLayer.containsKey(depth))
			listLayer.put(depth, new Layer());
		listLayer.get(depth).add(drawable);
	}

	public void setBackground(Background background) 
	{
		this.background = background;
	}
	
	public Selectable findSelected(int x, int y)
	{
		selectCanvas.clear();
		Object tab[] = listLayer.values().toArray();
		for(int i = tab.length-1; i >= 0; i--)
		{
			Layer layer = (Layer)tab[i];
			for(int j=layer.listDrawable.size()-1; j >= 0; j--)
			{
				Drawable drawable = layer.listDrawable.get(j);
				if(drawable instanceof Selectable && ((Selectable)drawable).isSelect(x, y))
				{
					if(selectCanvas.hasPixelRead())
					{
						((Selectable)drawable).selectDraw(selectCanvas);
						JsArrayInteger array = selectCanvas.getImageData(x, y, 1, 1);
						if(array.get(3) == 0)
							continue;
					}
					else System.out.println("can't read image data");
					
					return (Selectable)drawable;
				}
			}
		}
		return null;
	}
}
